$(() => {
    let keywords = GetQueryString('keys');
    let area_id = GetQueryString('area_id');
    searchReault(keywords, area_id);

    function searchReault(keywords, area_id) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Plat|index_search_result", 'keywords': keywords, 'area_id': area_id },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let company = res.data.company;
                    let ngoods = res.data.ngoods;
                    let goods = '';
                    let com = '';
                    let goodsTitle = '';
                    let comTitle = '';
                    // 更多商品
                    goodsTitle = `
                        <span>商品</span>
                        <a class="moreGoods" href="more_goods_n.html?keys=${keywords}&area_id=${area_id}">更多</a>
                    `;
                    $('.goodsTitle').html(goodsTitle);
                    // 更多企业
                    comTitle = `
                        <span>企业</span>
                        <a class="moreCompany" href="more_company_n.html?keys=${keywords}&area_id=${area_id}">更多</a>
                    `;
                    $('.comTitle').html(comTitle);

                    ngoods.list.forEach(item => {
                        goods += `
                            <a href="home_n_shopClassifyDetail.html?goods_id=${item.id}&area_id=${item.area_id}" class="shopList">
                                <img src="${item.goods_thumb == '' ? '../img/user.jpg' : item.goods_thumb}" alt="${item.goods_name}">
                                <div class="shopItem">
                                    <p>${item.goods_name}</p>
                                    <span>￥${item.goods_price}</span>
                                </div>
                            </a>
                        `;
                    });
                    // 商品
                    if (ngoods.list != '') {
                        $('.ngoods').html(goods);
                    } else {
                        $('.ngoods').html(`<p class="marT20" style="text-align:center;color:red;">未找到相关商品！！！</p>`);
                    }

                    // 企业
                    company.list.forEach(item => {
                        com += `
                    <div class="comList">
                        <div class="comHead">
                            <div class="comHeadLeft">
                                <img class="headImg" src="${item.company_logo == '' ? '../img/user.jpg' : item.company_logo}" alt="${item.company_name}">
                                <div class="headlineTopChild">
                                    <p>${item.company_name}</p>`
                                    if(item.level_logo != ''){
                                        html +=`<img src="${item.level_logo}" class="userLevel"  alt="${item.level_name}"/>`
                                    } 
                                html +=`</div>
                            </div>
                            <a class="comHeadRight" href="shop_details_company.html?company_id=${item.id}">进店</a>
                        </div>
                        <div class="comImgs">`

                        item.goods_list.forEach(g => {
                            com += `<a href="comp_shopDetail.html?id=${g.id}"><img class="comImg" src="${g.goods_thumb == '' ? '../img/user.jpg' : g.goods_thumb}" alt="${g.goods_name}"></a>`;
                        })
                        com += `</div>
                        <p class="comDesc">
                            主营：${item.main_business}
                        </p>
                        <p class="comAddr">
                            地址：${item.area_name + item.address}
                            <span>浏览量${item.read_count}</span>
                        </p>
                    </div>
                    `;
                    })
                    if (company.list != '') {
                        $('.companyList').html(com);
                    } else {
                        $('.companyList').html(`<p class="marT20" style="text-align:center;color:red;">未找到相关企业！！！</p>`);
                    }
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }


    $('.inhesearch').on('click', function() {
        let keys = $('.heinput').val();
        searchReault(keys, 0);
    })

    // 点击更多企业
    $('.moreCompany').on('click', function() {
        // console.log(111);
        ocation.href = "companyLists.html?keys=" + keywords + '&area_id=' + area_id;
    })
    // 点击更多商品
    $('.moreGoods').on('click', function() {
        // console.log(222);
        location.href = "more_googds_n.html?keys=" + keywords + '&area_id=' + area_id;
    })

    // 地区匹配
    // $('.cityBtn').on('click', function() {
    //     $.ajax({
    //         type: "post",
    //         url: url,
    //         data: { 'method': "Api|get_area_list", 'pcode': 1 },
    //         dataType: 'json',
    //         success: function(res) {
    //             // // console.log(res);
    //             let data = res.data;
    //             let html = '';
    //             if (res.status == 1) {
    //                 data.forEach(item => {
    //                     html += `
    //                         <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
    //                      `;
    //                 })
    //                 $('.cityLists').html(html);
    //             }
    //             $('.cityItem').on('click', function() {
    //                 let area_code = $(this).attr('data-code');
    //                 if (area_code) {
    //                     $('.comLists').html('');
    //                     companyLists(page, '', area_code, '');
    //                 }
    //             })
    //         }
    //     })
    // })
})